package com.linewow.xhyy.nihaonew.db;

import android.content.Context;
import android.util.Log;

import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.application.NiHaoApp;
import com.linewow.xhyy.nihaonew.entity.PushParams;
import com.linewow.xhyy.nihaonew.entity.db.AppInfo;
import com.linewow.xhyy.nihaonew.entity.db.StoreInfo;
import com.linewow.xhyy.nihaonew.entity.user.UserInfo;
import com.linewow.xhyy.nihaonew.event.MessageEvent;
import com.linewow.xhyy.nihaonew.net.api.NiHaoOkgo;
import com.litesuits.orm.LiteOrm;
import com.litesuits.orm.db.DataBaseConfig;
import com.umeng.analytics.MobclickAgent;

import org.simple.eventbus.EventBus;

import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
import rx.functions.Action1;
import rx.schedulers.Schedulers;


/**
 * Created by LXR on 2016/9/20.
 */
public class NiHaoHelper {
    private static NiHaoHelper instance;
    private LiteOrm liteOrm;

    private  NiHaoHelper() {
    }

    public static NiHaoHelper getInstance(){
        if(instance==null){
            synchronized (NiHaoHelper.class){
                if(instance==null){
                    instance=new NiHaoHelper();
                }
            }
        }
        return instance;
    }

    public void init(Context context){
        DataBaseConfig dataBaseConfig=new DataBaseConfig(context,"nihao.db");
        dataBaseConfig.dbVersion=1;
        dataBaseConfig.onUpdateListener=null;
        liteOrm=LiteOrm.newSingleInstance(dataBaseConfig);

    }

    public void login(AppInfo appInfo){
        liteOrm.save(appInfo);
        JpushLogin(appInfo);
        UmengLogin(appInfo);
    }

    private void UmengLogin(AppInfo appInfo) {
        MobclickAgent.onProfileSignIn(appInfo.uid);
    }

    public void JpushLogin(AppInfo appInfo){
        JPushInterface.setAlias(NiHaoApp.getInstance().getApplicationContext(), appInfo.uid, new TagAliasCallback() {
            @Override
            public void gotResult(int i, String s, Set<String> set) {
                Log.e("NiHaoHelper","设置请求完成了"+s+"---"+i);
            }
        });
    }

    /**
     * 个人主页里面的信息
     * @param userInfo
     */
    public void saveProfile(UserInfo userInfo){
        liteOrm.save(userInfo);
    }


    public UserInfo getUserProfile(){
        UserInfo userInfo=liteOrm.queryById(1,UserInfo.class);
        return  userInfo;
    }

    public void logOut(){
        liteOrm.save(new AppInfo("","null"));
        JPushInterface.setAlias(NiHaoApp.getInstance().getApplicationContext(), "0", new TagAliasCallback() {
            @Override
            public void gotResult(int i, String s, Set<String> set) {
                Log.e("NiHaoHelper","退出完成了"+s+"---"+i);
            }
        });
        MobclickAgent.onProfileSignOff();
    }


    public boolean getToken(){
       AppInfo appInfo=liteOrm.queryById(1,AppInfo.class);
       if(appInfo==null){
           return  false;
       }
       if(appInfo.token.equals("")){
           return  false;
       }else{
           return  true;
       }
   }
    public String getTokenContent(){
        AppInfo appInfo=liteOrm.queryById(1,AppInfo.class);
        if(appInfo==null){
            return  "";
        }else{
            return appInfo.token;
        }

    }
    public String getUidContent(){
        AppInfo appInfo=liteOrm.queryById(1,AppInfo.class);
        if(appInfo==null){
            return  "null";
        }else{
            return appInfo.uid;
        }

    }
    public AppInfo getAppInfo(){
        AppInfo appInfo=liteOrm.queryById(1,AppInfo.class);
        return  appInfo;
    }

    public void requestPush(String alias,String contents,String title,String rid){
        if(!alias.equals(NiHaoApp.uid)){
            PushParams pushParams=new PushParams(alias,contents,title,rid);
            NiHaoOkgo.pushInfo(pushParams)
                    .subscribeOn(Schedulers.io())
                    .observeOn(Schedulers.io())
                    .subscribe(new Action1<String>() {
                        @Override
                        public void call(String s) {
                            Log.e("NiHaoHelper","得到的"+s);
                        }
                    });
        }
    }

    public void requestZan(String alias,String contents,String rid){
        requestPush(alias,contents,"赞",rid);
    }
    public void requestZan(String alias,String rid){
        String contents=NiHaoApp.getInstance().getApplicationContext().getResources().getString(R.string.message_zan_content);
        requestPush(alias,contents,"赞",rid);
    }
    public void requestReply(String alias,String contents,String rid){
        requestPush(alias,contents,"回复",rid);
    }
    public void requestReply(String alias,String rid){
        String contents=NiHaoApp.getInstance().getApplicationContext().getResources().getString(R.string.message_reply_content);
        requestPush(alias,contents,"回复",rid);
    }
    public void requestAttention(String alias,String rid){
        String contents=NiHaoApp.getInstance().getApplicationContext().getResources().getString(R.string.message_attention_content);
        requestPush(alias,contents,"关注",rid);
    }
    public void requestAttention(String alias,String contents,String rid){
        requestPush(alias,contents,"关注",rid);
        EventBus.getDefault().post(new MessageEvent(),"refresh");
    }

    public int getStoreInfo(){

        StoreInfo storeInfo=liteOrm.queryById(1,StoreInfo.class);
        if(storeInfo==null){
            liteOrm.save(new StoreInfo(1));
            return 0;
        }else{
            return storeInfo.getTime();
        }
    }

    //得到忽略了的版本
    public String getVersionNumber(){
        StoreInfo storeInfo=liteOrm.queryById(1,StoreInfo.class);
        if(storeInfo==null){
            liteOrm.save(new StoreInfo(0));//不可能为空
            return null;
        }else{
            return storeInfo.getVersionNumber();
        }
    }
    //设置要忽略的版本
    public void setVersionNumber(String version){
        StoreInfo storeInfo=liteOrm.queryById(1,StoreInfo.class);
        if(storeInfo==null){
            StoreInfo storeInfo1=new StoreInfo(0);
            storeInfo1.setVersionNumber(version);
            liteOrm.save(storeInfo1);
        }else{
            storeInfo.setVersionNumber(version);
            liteOrm.save(storeInfo);
        }
    }
}
